package commons.ui.action;

import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;

/**
 * 
 */
public class WorkbenchWindowActionDelegate extends ActionDelegate implements IWorkbenchWindowActionDelegate {
    protected IWorkbenchWindow window;

    /**
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
     */
    @Override
    public void dispose() {
    }

    /**
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
     */
    public void init(IWorkbenchWindow w) {
	window = w;
    }

    public IWorkbenchWindow getWorkbenchWindow() {
	if (window == null) {
	    window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	}
	return window;
    }

    /**
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
     *      org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void selectionChanged(IAction action, ISelection selection) {
	// action.setEnabled(PropertyManager.checkPermission(action.getId()));
    }

    @Override
    public void run(IAction action) {
	SafeRunner.run(new SafeRunnable() {
	    @Override
	    public void run() throws Exception {
		WorkbenchWindowActionDelegate.this.run();
	    }
	});
    }

    public void run() throws Exception {
    }
}
